#!/bin/bash
# trims artifacts from folder of fastx.

# parameters.
indir=$1
outdir=$2

adapters[0]="AGCATACGGCAGAAGACGAAC"
adapters[1]="AATGATACGGCGACCACCGACAGGTTCAGAGTTCTACAGTCCGA"
adapters[2]="CGACAGGTTCAGAGTTCTACAGTCCGACGATC"
adapters[3]="GTTCAGAGTTCTACAGTCCGACGATC" 
adapters[4]="TCGTATGCCGTCTTCTGCTTGT" 
adapters[5]="CAAGCAGAAGACGGCATACGA"
adapters[6]="AATGATACGGCGACCACCGACAGGTTCAGAGTTCTACAGTCCGA" 
adapters[7]="CGACAGGTTCAGAGTTCTACAGTCCGACGATC"
adapters[8]="CAAGCAGAAGACGGCATACGA"

function process {
	# basename.
	x=$1
	bname=$(basename $x);
	tmp=${bname}.tmp
	tmp2=${bname}.tmp2
	echo "processing $bname"
	
	# remove artifacts.
	fastx_artifacts_filter -i $x -o $tmp -v	
	
	# trim all adapters.
	for adapt in ${adapters[@]}; do
		# remove adapter.
		fastx_clipper -a $adapt -M 8 -i $tmp -o $tmp2
		
		# replace original.
		mv $tmp2 $tmp
	done;
	
	# move to final location.
	mv $tmp ${outdir}/${bname}
}

# loop over indir.
for x in $(ls ${indir}/*); do
	process $x &
done;
